package com.ming.liteflow.core.node;

import com.ming.liteflow.core.enums.LiteFlowEnums;
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.core.NodeIfComponent;

public class CmpScriptUtil {

    public static LiteFlowNodeBuilder getLiteFlowNodeBuilder(NodeInfo info){
        String type = info.getType();
        switch (type){
            case "switch": return LiteFlowNodeBuilder.createSwitchNode();
            case "if": return LiteFlowNodeBuilder.createIfNode();
            case "for": return LiteFlowNodeBuilder.createForNode();
            case "while": return LiteFlowNodeBuilder.createWhileNode();
            case "break": return LiteFlowNodeBuilder.createBreakNode();
            case "iterator": return LiteFlowNodeBuilder.createIteratorNode();
            case "script": return LiteFlowNodeBuilder.createScriptNode();
            case "switch_script": return LiteFlowNodeBuilder.createScriptSwitchNode();
            case "if_script": return LiteFlowNodeBuilder.createScriptIfNode();
            case "for_script": return LiteFlowNodeBuilder.createScriptForNode();
            case "while_script": return LiteFlowNodeBuilder.createScriptWhileNode();
            case "break_script": return LiteFlowNodeBuilder.createScriptBreakNode();
            case "fallback": return null;//每种组件类型 只能定义一个降级组件
            default: return LiteFlowNodeBuilder.createCommonNode();
        }
    }

//    public static LiteFlowEnums.SCRIPT_JAVA getScriptJava(NodeInfo info){
//        String code = info.getNodeType().getCode();
//        switch (code){
//            case "script": return LiteFlowEnums.SCRIPT_JAVA.script;
//            case "switch_script": return LiteFlowEnums.SCRIPT_JAVA.switch_script;
//            case "if_script": return LiteFlowEnums.SCRIPT_JAVA.if_script;
//            case "for_script": return LiteFlowEnums.SCRIPT_JAVA.for_script;
//            case "while_script": return LiteFlowEnums.SCRIPT_JAVA.while_script;
//            case "break_script": return LiteFlowEnums.SCRIPT_JAVA.break_script;
//            default: return null;
//        }
//    }

    public static void createNode(NodeInfo nodeInfo){
        LiteFlowNodeBuilder builder = getLiteFlowNodeBuilder(nodeInfo);
        if(builder != null){
            createNode(builder, nodeInfo, null);
        }
    }

    public static void createNode(LiteFlowNodeBuilder builder, NodeInfo nodeInfo, LiteFlowEnums.SCRIPT_JAVA script){
        builder.setId(nodeInfo.getComponentId())
                .setName(nodeInfo.getComponentName())
                .setLanguage(nodeInfo.getLanguage())
                .setClazz(nodeInfo.getClazz())
                .setScript(nodeInfo.getScript())
                .build();
    }
}
